<template>
  <div class="statistics">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">数据统计</span>
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="fetchStatistics"
          />
        </div>
      </template>

      <el-row :gutter="20" v-loading="loading">
        <el-col :span="8">
          <el-statistic title="总订单数" :value="statistics.total_orders">
            <template #suffix>单</template>
          </el-statistic>
        </el-col>
        <el-col :span="8">
          <el-statistic title="总交易额" :value="statistics.total_revenue" :precision="2">
            <template #prefix>¥</template>
          </el-statistic>
        </el-col>
        <el-col :span="8">
          <el-statistic title="平均订单金额" :value="statistics.avg_order_price" :precision="2">
            <template #prefix>¥</template>
          </el-statistic>
        </el-col>
      </el-row>

      <el-divider />

      <el-row :gutter="20">
        <el-col :span="6">
          <el-statistic title="新增用户" :value="statistics.new_users || 0">
            <template #suffix>人</template>
          </el-statistic>
        </el-col>
        <el-col :span="6">
          <el-statistic title="新增商家" :value="statistics.new_stores || 0">
            <template #suffix>家</template>
          </el-statistic>
        </el-col>
        <el-col :span="6">
          <el-statistic title="新增外卖" :value="statistics.new_takeouts || 0">
            <template #suffix>个</template>
          </el-statistic>
        </el-col>
        <el-col :span="6">
          <el-statistic title="完成订单" :value="statistics.completed_orders || 0">
            <template #suffix>单</template>
          </el-statistic>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { adminApis } from '@/assets/apis/AdminApis'

const loading = ref(false)
const dateRange = ref([])
const statistics = ref({
  total_orders: 0,
  total_revenue: 0,
  avg_order_price: 0,
  new_users: 0,
  new_stores: 0,
  new_takeouts: 0,
  completed_orders: 0
})

const fetchStatistics = async () => {
  loading.value = true
  try {
    let startDate = ''
    let endDate = ''
    
    if (dateRange.value && dateRange.value.length === 2) {
      startDate = dateRange.value[0].toISOString().split('T')[0]
      endDate = dateRange.value[1].toISOString().split('T')[0]
    }
    
    const data = await adminApis.getStatistics(startDate, endDate)
    statistics.value = data
  } catch (error: any) {
    console.error('获取统计数据失败：', error)
    ElMessage.error(error.error || '获取统计数据失败')
  } finally {
    loading.value = false
  }
}

onMounted(() => {
  fetchStatistics()
})
</script>

<style scoped>
.statistics {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.el-row {
  margin-top: 20px;
}
</style>
